package com.tangzhe.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

/**
 * 测试get、post请求，request、response常见方法
 */
public class ServletDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("我是post请求");

        //获取请求参数
        //String name = request.getParameter("nameP");
        //解决post乱码问题
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("nameP");
        String pwd = request.getParameter("pwdP");
        System.out.println("post请求：name：" + name + ", pwd：" + pwd);

        String a = request.getContextPath();   //获取工程名
        String b = request.getRequestURI();    //获取请求路径
        String c = request.getRequestURL().toString();    //获取请求全路径
        String d = request.getRemoteAddr(); //获取请求ip地址
        String e = request.getRemoteHost(); //获取请求主机ip
        Map<String, String[]> map = request.getParameterMap();

        //写回一个页面
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("<html>");
        out.print("<head>");
        out.print("<title>servletDemo1</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("<h2>欢迎"+name+"</h2>");
        out.print("<div>"+new Date()+"</div>");
        out.print("</body>");
        out.print("</html>");

        //关闭流
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("我是get请求");

        //获取请求参数
        //解决get请求乱码问题
        //String name = new String(request.getParameter("name").getBytes("iso8859-1"), "utf-8");
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        System.out.println("get请求：name：" + name + ", pwd：" + pwd);
    }
}












